理论上Mirai Console也是可以使用的。IService那些由于适配了spring的事务,所以无spring环境用会有点困难。个人建议对于Iservice提供的快捷方法 如:saveOrUpdate 还是通过手写xml实现吧。
传送门:
Mybatis Plus Generator(3.5.1以下版本)
Mybatis Plus Generator(3.5.1及以上版本 ) 目标楼层末尾
pon.xml
新建一个SqlSessionConfig
public class SqlSessionConfig { public static SqlSession session = init(); private static SqlSession init() { SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); MybatisConfiguration configuration = new MybatisConfiguration(); initConfiguration(configuration); configuration.addInterceptor(initInterceptor()); //扫描DAO/Mapper所在包 configuration.addMappers("org.test.mybatis.entity.dao"); //配置日志实现 configuration.setLogImpl(Slf4jImpl.class); //构建mybatis-plus需要的globalconfig GlobalConfig globalConfig = new GlobalConfig(); //此参数会自动生成实现baseMapper的基础方法映射 globalConfig.setSqlInjector(new DefaultSqlInjector()); //设置id生成器 globalConfig.setIdentifierGenerator(new DefaultIdentifierGenerator()); //设置超类mapper globalConfig.setSuperMapperClass(BaseMapper.class); // 这里可以配置逻辑删除 globalConfig.setDbConfig(initDBConfig()); //给configuration注入GlobalConfig里面的配置 GlobalConfigUtils.setGlobalConfig(configuration, globalConfig); //设置数据源 Environment environment = new Environment("1", new JdbcTransactionFactory(), initDataSource()); configuration.setEnvironment(environment); // 设置xxxDAO.xml所在的目录 // registryMapperXml(configuration, "dao"); //构建sqlSessionFactory SqlSessionFactory sqlSessionFactory = builder.build(configuration); return sqlSessionFactory.openSession(true); } private static void initConfiguration(MybatisConfiguration configuration) { configuration.setMapUnderscoreToCamelCase(true); configuration.setUseGeneratedKeys(true); } private static DataSource initDataSource() { HikariDataSource dataSource = new HikariDataSource(); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/back_up?useSSL=false&autoReconnect=true&serverTimezone=GMT%2B8&rewriteBatchedStatements=true"); dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); dataSource.setUsername("root"); dataSource.setPassword("123456"); // 数据源设置,请按照当前DB参数设置 dataSource.setIdleTimeout(60000); dataSource.setAutoCommit(true); dataSource.setMaximumPoolSize(5); dataSource.setMinimumIdle(1); dataSource.setMaxLifetime(60000 * 10); dataSource.setConnectionTestQuery("SELECT 1"); return dataSource; } private static Interceptor initInterceptor() { //创建mybatis-plus插件对象 MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); //构建分页插件 PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(); paginationInnerInterceptor.setDbType(DbType.MYSQL); paginationInnerInterceptor.setOverflow(true); paginationInnerInterceptor.setMaxLimit(500L); interceptor.addInnerInterceptor(paginationInnerInterceptor); return interceptor; } private static GlobalConfig.DbConfig initDBConfig() { GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig(); // 逻辑删除字段 dbConfig.setLogicDeleteField("enable"); dbConfig.setLogicDeleteValue("0"); dbConfig.setLogicNotDeleteValue("1"); return dbConfig; } private static void registryMapperXml(MybatisConfiguration configuration, String classPath) throws IOException { ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); Enumeration<URL> mapper = contextClassLoader.getResources(classPath); while (mapper.hasMoreElements()) { URL url = mapper.nextElement(); if (url.getProtocol().equals("file")) { String path = url.getPath(); File file = new File(path); File[] files = file.listFiles(); for (File f : files) { FileInputStream in = new FileInputStream(f); XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(in, configuration, f.getPath(), configuration.getSqlFragments()); xmlMapperBuilder.parse(); in.close(); } } else { JarURLConnection urlConnection = (JarURLConnection) url.openConnection(); JarFile jarFile = urlConnection.getJarFile(); Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { JarEntry jarEntry = entries.nextElement(); if (jarEntry.getName().endsWith(".xml")) { InputStream in = jarFile.getInputStream(jarEntry); XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(in, configuration, jarEntry.getName(), configuration.getSqlFragments()); xmlMapperBuilder.parse(); in.close(); } } } } } }用法:
SqlSession session = SqlSessionConfig.session; TImageDao tImageDao = session.getMapper(TImageDao.class); List<TImage> tImages = tImageDao.selectList(null); log.info("******{}", tImages);